/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db.server.DAO;



import db.server.DTO.DesignTableDTO;
import db.server.DTO.EditTableDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 *
 * @author QuangKhai
 */
public class EditDAO {
    private Document document = null;
    
    /** Creates new class EditDAO */
    public EditDAO(){
        
        DocumentBuilder builder = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();    
        
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(EditDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        document = builder.newDocument();        
    }
    
    ///Save value of edit table into xml file
    public void writeEditTable(List<String> lstValue, DesignTableDTO tableDTO, String fileName) throws ParserConfigurationException{
        // Insert Root Table
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document dcmt = builder.newDocument();
            
        if(fileName == null){            
            Element root = (Element) dcmt.createElement("Table");      
            root.setAttribute("name", tableDTO.getTableName());
            dcmt.appendChild(root);

            
            int columnCount = tableDTO.getLstRows().size();
            int rowCount = (lstValue.size() - 1)/columnCount;
            int index = 1;
            
            for(int i = 0; i < rowCount; i++){            
                Element row = (Element) dcmt.createElement("Row");

                for(int j = 0; j < columnCount; j++){                
                    row.setAttribute(tableDTO.getLstRows().get(j).getColumnName().toString(), lstValue.get(index));                
                    index++;
                }
                
                root.appendChild(row);
            }

            // Normalizing the DOM
            dcmt.getDocumentElement().normalize();       
            DOMUtil.writeXmlToFile(lstValue.get(0), dcmt);
        }else{
            Element root = (Element) dcmt.createElement("Table");                 
            dcmt.appendChild(root);
            DOMUtil.writeXmlToFile(fileName, dcmt);
        }        
    }
    
    ///Read edit xml file to get value
    public EditTableDTO readEditTable(String editPath, DesignTableDTO tableDTO){
        EditTableDTO editTableDTO = new EditTableDTO();
        
        editTableDTO.setEditTablePath(editPath);
        List<String> lstColumnName = new ArrayList<String>();
        List<List<String>> lstValues = new ArrayList<List<String>>();
        
        document = DOMUtil.parse(editPath);        
        Element root = document.getDocumentElement();        
        NodeList childNode = root.getChildNodes();        
        int num = childNode.getLength();
        
        for(int i = 0; i < tableDTO.getLstRows().size(); i++){
            lstColumnName.add(tableDTO.getLstRows().get(i).getColumnName());
        }
        
        for(int i = 0; i < num; i++){
            Element node = (Element) childNode.item(i);
            
            List<String> value = new ArrayList<String>();
            for(int j = 0; j < tableDTO.getLstRows().size(); j++){
                //lstValues.add(node.getAttribute(tableDTO.getLstRows().get(j).getColumnName().toString()));
                value.add(node.getAttribute(tableDTO.getLstRows().get(j).getColumnName().toString()));                       
            }
            
            lstValues.add(value);
        }                
        
        editTableDTO.setColumnName(lstColumnName);
        editTableDTO.setLstValues(lstValues);
        
        return editTableDTO;
    }
}
